"""
服务健康检查脚本
"""
import asyncio
import httpx
import time

async def check_service_health():
    """检查服务健康状态"""
    base_url = "http://localhost:8000"
    
    print("🔍 检查服务状态...")
    
    try:
        async with httpx.AsyncClient(timeout=10.0) as client:
            # 检查基础健康接口
            response = await client.get(f"{base_url}/health")
            
            if response.status_code == 200:
                health_data = response.json()
                print("✅ 服务健康检查通过！")
                print(f"📊 系统状态: {health_data.get('status', 'unknown')}")
                
                services = health_data.get('services', {})
                for service, status in services.items():
                    status_icon = "✅" if status in ["connected", "available", "healthy"] else "❌"
                    print(f"   {status_icon} {service}: {status}")
                
                return True
            else:
                print(f"❌ 健康检查失败: HTTP {response.status_code}")
                return False
                
    except httpx.ConnectError:
        print("❌ 无法连接到服务，请检查服务是否启动")
        return False
    except Exception as e:
        print(f"❌ 健康检查出错: {e}")
        return False

async def check_web_interface():
    """检查Web界面"""
    base_url = "http://localhost:8000"
    
    try:
        async with httpx.AsyncClient(timeout=10.0) as client:
            response = await client.get(base_url)
            
            if response.status_code == 200:
                print("✅ Web界面可访问")
                return True
            else:
                print(f"❌ Web界面访问失败: HTTP {response.status_code}")
                return False
                
    except Exception as e:
        print(f"❌ Web界面检查出错: {e}")
        return False

def main():
    """主函数"""
    print("=" * 50)
    print("🚀 微信公众号文章生成工具 - 服务健康检查")
    print("=" * 50)
    
    # 等待服务启动
    print("⏳ 等待服务启动...")
    time.sleep(3)
    
    # 健康检查
    health_ok = asyncio.run(check_service_health())
    web_ok = asyncio.run(check_web_interface())
    
    print("\n" + "=" * 50)
    if health_ok and web_ok:
        print("🎉 开发环境重启成功！")
        print("🌐 访问地址: http://localhost:8000")
        print("📚 API文档: http://localhost:8000/docs")
    else:
        print("⚠️  服务启动可能存在问题，请检查日志")
    print("=" * 50)

if __name__ == "__main__":
    main()